<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

use think\Session;
use think\Request;
use think\Cache;
use think\Config;
use think\Db;
use think\Controller;
use \CFormat;
use think\View;
use PHPExcel;
use PHPExcel_Writer_Excel5;

class Order  extends Controller {
    // 视图类实例
    protected $view;
    // Request实例
    protected $request;

    public function __construct()
    {
        parent::__construct();

        if (null === $this->view) {
            $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
        }
        if (null === $this->request) {
            $this->request = Request::instance();
        }
        // 用户ID
        defined('UID') or define('UID', Session::get(Config::get('rbac.user_auth_key')));
    }
    
    public function index(){
        $startdate= input('post.startdate');
        $enddate= input('post.enddate');
        
        $time= time();
        $start= strtotime($startdate);
        $end= strtotime($enddate)+3600*24;
        $today = strtotime(date('Y-m-d', $time));  //获取今天0时0分0秒的时间戳
        
        $where = $oi_where = array();
        if(!$startdate&&!$enddate){
            $start = $today;
            $where['gen_time'] = ['>=', $start];
        }
        else if(!$startdate&&$enddate) {
            $where['gen_time'] = ['<', $end];
        }
        else if(!$enddate&&$startdate) {
            $where['gen_time'] = ['>=', $start];
        }
        else {
            if($startdate > $enddate){
                return ajax_return_adv_error('开始时间不能大于结束时间');
            }
            $where['gen_time'] = ['>=', $start];
            $where['gen_time'] = ['<', $end];
        }
        $where['status'] = 1;
        //总订单数、总金额
        $order = Db::name('order')
                ->field('COUNT(id) AS count,SUM(payment) AS reciept')
                ->where($where)
                ->find();
        $oi_where['gen_time'] = $where['gen_time'];
        $products = Db::name('order_item')
                ->field('product_name AS pname,SUM(count) AS pcount,SUM(total_price) AS price')
                ->where($oi_where)
                ->group('product_name')
                ->paginate(15);
        $allproduct = Db::name('order_item')
                ->field('product_name AS pname,SUM(count) AS pcount,SUM(total_price) AS price')
                ->where($oi_where)
                ->group('product_name')
                ->select();
        Session::set('products', $allproduct);
        
        $page = $products->render();
        
        $this->view->assign('order', $order);
        $this->view->assign('products', $products);
        $this->view->assign('start', $startdate);
        $this->view->assign('end', $enddate);
        $this->view->assign('page', $page);
        return $this->view->fetch();
    }
    
    public function toExcel() {
        $products= Session::get('products');
        Session::delete('products');
        $head=array('编号','商品名称','销售数量','销售金额');
        $name='超市经营状况';
        $count= count($products);
        //创建一个excel
        $objPHPExcel = new \PHPExcel();
        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 
        //设置当前的sheet
        $objPHPExcel->setActiveSheetIndex(0);
        //设置sheet的name
        $objPHPExcel->getActiveSheet()->setTitle('商品售卖情况');
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);
        $objPHPExcel->getActiveSheet()->mergeCells('A1:D1');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical('center');
        $objPHPExcel->getActiveSheet()->setCellValue('A1', $name);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(15);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        $c='A';
        for ($i=0;$i<4;$i++){
            $objPHPExcel->getActiveSheet()->getColumnDimension($c)->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->setCellValue($c++ . 2, $head[$i]);
        }
        for($j=3;$j<=$count+2;$j++){
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $j, $j-2);
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $j, $products[$j-3]['pname']);
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $j, $products[$j-3]['pcount']);
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $j, $products[$j-3]['price']);
        }
        output_excel($objWriter, $name);
    }
}
